import { search } from "@/services/search"
import { ref, watch } from 'vue'
import { formatSearchSongs } from '@/utils/util'
import { usePlayStore } from '@/stores'
import { storeToRefs } from 'pinia'

export const useSearchResults = (type) => {
  const playStore = usePlayStore()
  const list = ref([])
  const loading = ref(false)
  const finished = ref(false)
  const page = ref(0)
  const empty = ref(false)
  const { searchKey } = storeToRefs(usePlayStore())

  const onLoad = async () => {
    loading.value = true
    await loadData(searchKey.value, page.value)
    page.value++
  }

  const loadData = async (keyword, page) => {
    let res
    switch (type) {
      case 'songs':
        res = await search(keyword, 30, page * 30, 1)
        list.value = [...list.value, ...formatSearchSongs(res.result?.songs)]
        if (list.value.length >= res.result.songCount) finished.value = true
        loading.value = false
        break
      case 'video':
        res = await search(keyword, 30, page * 30, 1004)
        if (res.result.mvCount) {
          list.value = [...list.value, ...res.result?.mvs]
        } else {
          list.value = []
          empty.value = true
          finished.value = true
          loading.value = false
        }
        if (list.value.length >= res.result.mvCount) finished.value = true
        loading.value = false
        break
      case 'user':
        res = await search(keyword, 30, page * 30, 1002)
        if (res.result.userprofileCount) list.value = [...list.value, ...res.result?.userprofiles]
        if (list.value.length >= res.result.artistCount) finished.value = true
        loading.value = false
        break
      case 'singer':
        res = await search(keyword, 30, page * 30, 100)
        if (res.result) list.value = [...list.value, ...res.result.artists]
        if (list.value.length >= res.result.artistCount) finished.value = true
        loading.value = false
        break
      case 'playlist':
        res = await search(keyword, 30, page * 30, 1000)
        list.value = [...list.value, ...res.result?.playlists]
        if (list.value.length >= res.result.playlistCount) finished.value = true
        loading.value = false
        break
      case 'album':
        res = await search(keyword, 30, page * 30, 10)
        if (res.result.albumCount) list.value = [...list.value, ...res.result?.albums]
        if (list.value.length >= res.result.albumCount) finished.value = true
        loading.value = false
        break;
      default:
        break;
    }
  }

  const highlightKeyword = (name) => {
    const regex = new RegExp(searchKey.value, 'gi')
    return name.replace(regex, '<span style="color: #5983b2;">$&</span>')
  }

  const playAll = () => {
    playStore.selectPlay(list.value.filter(i => !i.vip))
    playStore.setPlayStatus(false)
    playStore.changeMusicDialog(true)
  }

  watch(() => searchKey.value, async newVal => {
    if (newVal !== '') {
      list.value = []
      await loadData(newVal, 0)
    }
  })

  return {
    list,
    loading,
    finished,
    empty,
    searchKey,
    highlightKeyword,
    playAll,
    onLoad
  }
}